//
//  MAUserInfoViewController.m
//  meiang_edu
//
//  Created by hfcb on 2018/9/7.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MAUserInfoViewController.h"
#import "MAUserEditInfoController.h"

#import "WGQChangePasswordViewController.h"//修改密码
#import "WGQMyQRCodeViewController.h"//我的w二维码
#import "JPUSHService.h"

@interface MAUserInfoViewController ()
@property (weak, nonatomic) IBOutlet UIView *containerVIew;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *mainTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *subTitleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *bgIconView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgViewHeightCons;

@end

@implementation MAUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setupBasic];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self hiddenTabbar:YES];
    [self bindInfo];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)bindInfo {
    MAUserInfo *info = [MAUserInfoTool userinfo];
    [self.bgIconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,info.cbackground]] placeholderImage:[UIImage imageNamed:@""]];
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,info.ps_id]] placeholderImage:[UIImage imageNamed:@"homepage_navigationbar_picture"]];

    self.mainTitleLabel.text = info.name;
    self.subTitleLabel.text = @"";
}

- (void)setupBasic {
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self wr_setNavBarBackgroundAlpha:0];
    self.containerVIew.layer.cornerRadius = 5;
    self.containerVIew.layer.masksToBounds = YES;
    
    if (IS_IPHONE_X) {
        self.bgViewHeightCons.constant = 250;
    }
    
    for (int i=0; i<self.containerVIew.subviews.count; i++) {
        UIView *view = self.containerVIew.subviews[i];
        [view addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewDidTap:)]];
        
    }
}

- (void)viewDidTap:(UITapGestureRecognizer *)tap {
    switch (tap.view.tag) {
        case 1: { // 详情编辑
            MAUserEditInfoController *vc = [[UIStoryboard storyboardWithName:NSStringFromClass([MAUserEditInfoController class]) bundle:nil] instantiateInitialViewController];
            
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 2: // DLog(@"创建家庭组");
        {
            MAUserInfo *info = [MAUserInfoTool userinfo];
            if ([info.hasfamily isEqualToString:@"0"])
            {
                MAUserInfo *userinfo = [MAUserInfoTool userinfo];
                [ZBRequestManager requestWithConfig:^(ZBURLRequest *request)
                 {
                     request.requestSerializerType = ZBJSONRequestSerializer;
                     NSString * urlStr = [NSString stringWithFormat:@"%@%@",GREAT_FAMILY_GROUP,info.ID];
                     request.URLString = urlStr;
                     request.methodType = ZBMethodTypePOST;
                     [request setValue:userinfo.token forHeaderField:@"token"];
                     request.apiType = ZBRequestTypeRefresh;
                 } success:^(id responseObject, apiType type)
                 {
                     [MBManager hideAlert];
                     // 解析数据
                     NSDictionary * dataDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                     DLog(@"%@",dataDict);
                     NSInteger code = [dataDict[@"msgcode"] integerValue];
                     if (code == 1)
                     {
                         // 请求成功
                         [MBManager hideAlert];
                         UIAlertController * vc = [UIAlertController alertControllerWithTitle:@"提示" message:@"家庭组创建成功，请重新登录" preferredStyle:UIAlertControllerStyleAlert];
                         UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                         {
                             MAUserInfo *userinfo = [MAUserInfoTool userinfo];
                             [ZBRequestManager requestWithConfig:^(ZBURLRequest *request)
                              {
                                  request.requestSerializerType = ZBJSONRequestSerializer;
                                  NSString * urlStr = [NSString stringWithFormat:@"%@",USER_LOGOUT];
                                  request.URLString = urlStr;
                                  request.methodType = ZBMethodTypeGET;
                                  [request setValue:userinfo.token forHeaderField:@"token"];
                                  request.apiType = ZBRequestTypeRefresh;
                              } success:^(id responseObject, apiType type)
                              {
                                  [MBManager hideAlert];
                                  // 解析数据
                                  NSDictionary * dataDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                                  DLog(@"%@",dataDict);
                                  NSInteger code = [dataDict[@"msgcode"] integerValue];
                                  if (code == 1)
                                  {
                                      // 请求成功
                                      [MBManager hideAlert];
                                      [MBManager showBriefAlert:dataDict[@"message"]];
                                      [MAUserInfoTool deleteUserInfo];
                                      [self.navigationController popViewControllerAnimated:YES];
                                      [[NSNotificationCenter defaultCenter] postNotificationName:@"user_logout" object:nil];
                                  }
                                  else
                                  {
                                      [MBManager showBriefAlert:dataDict[@"message"]];
                                  }
                              } failure:^(NSError *error)
                              {
                                  DLog(@"%@",error);
                                  
                              } finished:^(id responseObject, apiType type, NSError *error, BOOL isCache)
                              {
                                  if (isCache==YES)
                                  {
                                      DLog(@"使用了缓存");
                                  }
                              }];
                         }];
                         [vc addAction:sureAction];
                         [self presentViewController:vc animated:YES completion:^{
                         }];
                     }
                     else
                     {
                         [MBManager showBriefAlert:dataDict[@"message"]];
                     }
                 } failure:^(NSError *error)
                 {
                     DLog(@"%@",error);
                     
                 } finished:^(id responseObject, apiType type, NSError *error, BOOL isCache)
                 {
                     if (isCache==YES)
                     {
                         DLog(@"使用了缓存");
                     }
                 }];
            }
            else
            {
                [MBManager showBriefAlert:@"当前已创建家庭组"];

            }
            break;
        }
        case 3: // DLog(@"关注列表");
        {
            [MBManager showBriefAlert:@"此功能尚未开通，敬请期待……"];
        }
            break;
        case 4: // DLog(@"我的二维码");
        {
            WGQMyQRCodeViewController * vc = [[WGQMyQRCodeViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 5: // DLog(@"修改密码");
        {
            WGQChangePasswordViewController * vc = [[WGQChangePasswordViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 6: // DLog(@"退出登录");
        {
            MAUserInfo *userinfo = [MAUserInfoTool userinfo];
            [ZBRequestManager requestWithConfig:^(ZBURLRequest *request)
             {
                 request.requestSerializerType = ZBJSONRequestSerializer;
                 NSString * urlStr = [NSString stringWithFormat:@"%@",USER_LOGOUT];
                 request.URLString = urlStr;
                 request.methodType = ZBMethodTypeGET;
                 [request setValue:userinfo.token forHeaderField:@"token"];
                 request.apiType = ZBRequestTypeRefresh;
             } success:^(id responseObject, apiType type)
             {
                 [MBManager hideAlert];
                 // 解析数据
                 NSDictionary * dataDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                 DLog(@"%@",dataDict);
                 NSInteger code = [dataDict[@"msgcode"] integerValue];
                 if (code == 1)
                 {
                     // 请求成功
                     [MBManager hideAlert];
                     [MBManager showBriefAlert:dataDict[@"message"]];
                     [MAUserInfoTool deleteUserInfo];
                     [self.navigationController popViewControllerAnimated:YES];
                     [[NSNotificationCenter defaultCenter] postNotificationName:@"user_logout" object:nil];
                     [JPUSHService cleanTags:^(NSInteger iResCode, NSSet *iTags, NSInteger seq)
                     {
                         DLog(@"%tu,%@,%tu",iResCode,iTags,seq);
                     } seq:1];
                 }
                 else
                 {
                     [MBManager showBriefAlert:dataDict[@"message"]];
                 }
             } failure:^(NSError *error)
             {
                 DLog(@"%@",error);
                 
             } finished:^(id responseObject, apiType type, NSError *error, BOOL isCache)
             {
                 if (isCache==YES)
                 {
                     DLog(@"使用了缓存");
                 }
             }];
            break;
        }
            
        default:
            break;
    }
}

@end
